package com.baseweb.webadmin.service.impl;

import com.baseweb.webadmin.core.model.dao.LogDao;
import com.baseweb.webadmin.core.model.mapper.LogMapper;
import com.baseweb.webadmin.core.model.pojo.Log;
import com.baseweb.webadmin.service.LogService;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class LogServiceImpl implements LogService {
    private Logger logger = LoggerFactory.getLogger(LogServiceImpl.class);
    @Autowired
    private LogMapper logMapper;

    @Autowired
    private LogDao logDao;

    @Override
    public int insert(Log log) {
        log.setLogTime(new DateTime());
        Subject currentUser = SecurityUtils.getSubject();
        if (currentUser != null && currentUser.getPrincipal() != null) {
            log.setUserName((String) currentUser.getPrincipal());
        }
        return logMapper.insert(log);
    }

    public int insert(String logType, String logContent, String ip) {
        if (StringUtils.isNotBlank(logType)) {
            Log log = new Log();
            log.setLogType(logType);
            log.setIp(ip);
            log.setLogContent(logContent);
            return insert(log);
        } else {
            logger.debug("未定义类型的操作");
            return -1;
        }
    }

    public int insert(String logType, String logContent, String ip, String username) {
        if (StringUtils.isNotBlank(logType)) {
            Log log = new Log();
            log.setLogType(logType);
            log.setIp(ip);
            log.setLogContent(logContent);
            log.setUserName(username);
            return insert(log);
        } else {
            logger.debug("未定义类型的操作");
            return -1;
        }
    }

    @Override
    public Integer getLogsCount(String username, String datetime, String logtype) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("whereString", getWheresString(username, datetime, logtype));
        return logDao.getLogsCount(map);
    }

    @Override
    public List<Log> getLogsList(int page, int pagesize, String username, String datetime, String logtype) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("offset", (page - 1) * pagesize);
        map.put("rows", pagesize);
        map.put("whereString", getWheresString(username, datetime, logtype));

        return logDao.getLogsList(map);
    }

    private String getWheresString(String username, String datetime, String logtype) {
        String where = "";
        if (StringUtils.isNotBlank(username)) {
            where += " and user_name like '%" + username + "%'";
        }
        if (datetime != null && StringUtils.isNotBlank(datetime)) {
            where += " and date(log_time)='" + datetime + "'";
        }
        if (StringUtils.isNotBlank(logtype)) {
            where += " and log_type='" + logtype + "'";
        }

        if (StringUtils.isNotBlank(where)) {
            where = " where" + where;
        }
        where = where.replace("where and", "where");
        return where;
    }
}
